import { useState } from 'react'
import { getWarehouseListBySearch } from '@/api/purchase'
export interface IWareHouseType {
  warehSn: string
  warehName: string
}
const useSearchWarehouse = () => {
  let FabricTimeout: ITimeoutType
  const [WareHouseOptions, setWareHouseOptions] = useState<IOptionItem[]>([])
  const handleSearchWareHouse = (newValue = '') => {
    if (FabricTimeout) {
      clearTimeout(FabricTimeout)
      FabricTimeout = null
    }
    const getOptions = async () => {
      const res = await getWarehouseListBySearch<IWareHouseType>({
        searchName: newValue,
      })
      if (res.code === 200) {
        const data =
          res?.data?.map((item) => {
            return {
              label: item.warehName,
              value: item.warehSn,
            }
          }) || []
        setWareHouseOptions(data)
      }
    }
    FabricTimeout = setTimeout(getOptions, 500)
  }
  return { WareHouseOptions, setWareHouseOptions, handleSearchWareHouse }
}

export default useSearchWarehouse
